package su.msk.jet.team01.chatik.server.commands;

import su.msk.jet.team01.chatik.server.data.Client;
import su.msk.jet.team01.chatik.server.data.DataManager;
import su.msk.jet.team01.chatik.server.data.Room;
import su.msk.jet.team01.chatik.server.exception.InvalidArgumentException;

public class ChangeRoomCommand extends Command {
	private static final int MAX_ROOM_NAME_LENGTH = 100;
	private static final boolean ALLOW_SPACES_IN_ROOM_NAMES = false;

	public ChangeRoomCommand() {
		super("room", 1);
	}

	@Override
	public void execute(final Client client, final String[] args)
			throws InvalidArgumentException {
		String name = args[0];
		Room room;

		if (!ALLOW_SPACES_IN_ROOM_NAMES) {
			validateHaveNoSpaces(name);
		}

		try {
			validateLength(name, MAX_ROOM_NAME_LENGTH);
			room = DataManager.getRoom(name);
			client.changeRoom(room);
			client.send("Room successfully changed to %s.", name);
		} catch (InvalidArgumentException e) {
			client.send(
					"Room name is too long. Maximum room name length is %d.",
					MAX_ROOM_NAME_LENGTH);
		}
	}
}
